﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PowersourceUIToolkit;
using UCL.Powersource;
using UCL.Powersource.ViewModels;

namespace Powersource_UnitTests
{
    [TestClass]
    public class MainViewModelTest
    {
        private delegate void ThreadInvoker();

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        PowersourcePackage package;
        UCL.Powersource.ViewModels.PowersourceMainViewModel ViewModel, newViewModel;

        //[TestInitialize]
        //public void TestInitialize()
        //{
        //}
        //[TestCleanup]
        //public void CleanUp()
        //{
        //}

        [TestMethod]
        [HostType("VS IDE")]
        public void TestSerialization()
        {
            package = new PowersourcePackage();
            OleServiceProvider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices();
            ((IVsPackage)package).SetSite(serviceProvider);
            ViewModel = ViewModelLocator.GetViewModel<PowersourceMainViewModel>();
            ViewModel.Activate();
            ViewModel.TryTFSConnect();
            var tfsconfig = ViewModel.TfsTeamProjectCollectionConfigurations;
            Dictionary<string, bool> dict = new Dictionary<string, bool>();
            if (tfsconfig != null)
            {
                foreach (var tcc in tfsconfig.TFSTeamCollectionConfigurations)
                {
                    foreach (var tpc in tcc.TeamProjectConfigurations)
                    {
                        if (tpc.IsChosen) tpc.IsChosen = false;
                        else tpc.IsChosen = true;
                        dict.Add(tcc.Uri+tpc.UniqueName, tpc.IsChosen);
                    }
                }
            }
            ViewModel.Deactivate();
            ((IVsPackage)package).Close();
            package = new PowersourcePackage();
            ((IVsPackage)package).SetSite(serviceProvider);
            newViewModel = ViewModelLocator.GetViewModel<PowersourceMainViewModel>();
            Assert.AreNotEqual(ViewModel, newViewModel);
            newViewModel.Activate();
            newViewModel.TryTFSConnect();
            tfsconfig = newViewModel.TfsTeamProjectCollectionConfigurations;
            if (tfsconfig != null)
            {
                foreach (var tcc in tfsconfig.TFSTeamCollectionConfigurations)
                {
                    foreach (var tpc in tcc.TeamProjectConfigurations)
                    {
                        if (dict.ContainsKey(tcc.Uri + tpc.UniqueName))
                            Assert.AreEqual(dict[tcc.Uri + tpc.UniqueName], tpc.IsChosen);
                    }
                }
            }
        }
    }
}
